﻿using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
using IdentityRole = Volo.Abp.Identity.IdentityRole;

namespace AbpProjectTemplate.Identity
{
    public class IdentityDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        protected IGuidGenerator GuidGenerator { get; }
        protected IIdentityRoleRepository RoleRepository { get; }
        protected IIdentityUserRepository UserRepository { get; }
        protected ILookupNormalizer LookupNormalizer { get; }
        protected IdentityUserManager UserManager { get; }
        protected IdentityRoleManager RoleManager { get; }
        protected ICurrentTenant CurrentTenant { get; }
        protected IOptions<IdentityOptions> IdentityOptions { get; }

        public IdentityDataSeedContributor(IGuidGenerator guidGenerator, IIdentityRoleRepository roleRepository, IIdentityUserRepository userRepository, ILookupNormalizer lookupNormalizer, IdentityUserManager userManager, IdentityRoleManager roleManager, ICurrentTenant currentTenant, IOptions<IdentityOptions> identityOptions)
        {
            GuidGenerator = guidGenerator;
            RoleRepository = roleRepository;
            UserRepository = userRepository;
            LookupNormalizer = lookupNormalizer;
            UserManager = userManager;
            RoleManager = roleManager;
            CurrentTenant = currentTenant;
            IdentityOptions = identityOptions;
        }

        public async Task CreateStaticRole(Guid? tenantId, string roleName, string desc)
        {
            var role =
                await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(roleName));
            if (role == null)
            {
                role = new IdentityRole(
                    GuidGenerator.Create(),
                    roleName,
                    tenantId
                )
                {
                    IsStatic = true,
                    IsPublic = true
                };
                role.SetDesc(desc);
                (await RoleManager.CreateAsync(role)).CheckErrors();
            }
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            var tenantId = context.TenantId;
            using (CurrentTenant.Change(tenantId))
            {
                var adminRole = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("admin"));
                if (adminRole != null)
                {
                    adminRole.SetDesc("系统超级管理员");
                    (await RoleManager.UpdateAsync(adminRole)).CheckErrors();
                }
                //await CreateStaticRole(tenantId, AppRoleConsts.ManagerRole, "基础管理员角色");
                //await CreateStaticRole(tenantId, AppRoleConsts.MemberRole, "基础用户角色");
            }
        }
    }
}
